package com.PengGoBook.main.adapter;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import com.PengGoBook.main.BooksCategoryListEntity;
import com.PengGoBook.main.entity.BlogsCategoryListEntity;
import com.PengGoBook.main.entity.BooksContentItem;
import com.PengGoBook.main.entity.CategorysEntity;
import com.PengGoBook.main.entity.WikiCategoryListEntity;
import com.PengGoBook.main.ui.BlogFragment;
import com.PengGoBook.main.ui.BooksFragment;
import com.PengGoBook.main.ui.HttpErrorFragment;
import com.PengGoBook.main.ui.WikiFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-5-27
 * Time: 上午1:13
 * To change this template use File | Settings | File Templates.
 */
public class BasePageAdapter extends FragmentStatePagerAdapter {

    public ArrayList<Fragment> mFragments = new ArrayList<Fragment>();;
    public List<CategorysEntity> tabs = new ArrayList<CategorysEntity>();

    private Activity mActivity;

    public BasePageAdapter(FragmentActivity activity) {
        super(activity.getSupportFragmentManager());
        this.mActivity = activity;
    }

    public void addFragment(List<CategorysEntity> mList, List<Object> listObject) {
        tabs.addAll(mList);
        for (int i = 0; i < listObject.size(); i++) {
            Object object = listObject.get(i);
            if (object instanceof BooksCategoryListEntity) {
                addTab(new BooksFragment(mActivity,
                        ((BooksCategoryListEntity) listObject.get(i))));
            } /*else if (object instanceof BlogsCategoryListEntity) {
                addTab(new BlogFragment(mActivity,
                        ((BlogsCategoryListEntity) listObject.get(i))));
            } else if (object instanceof WikiCategoryListEntity) {
                addTab(new WikiFragment(mActivity,
                        ((WikiCategoryListEntity) listObject.get(i))));
            }*/
        }
    }
    /**
     * 只加载listview不包含 tabs
     * @param listObject
     */
    public void addFragment( List<Object> listObject) {

        for (int i = 0; i < listObject.size(); i++) {
            Object object = listObject.get(i);
            if (object instanceof BooksCategoryListEntity) {
                addTab(new BooksFragment(mActivity,
                        ((BooksCategoryListEntity) listObject.get(i))));
            } /*else if (object instanceof BlogsCategoryListEntity) {
                addTab(new BlogFragment(mActivity,
                        ((BlogsCategoryListEntity) listObject.get(i))));
            } else if (object instanceof WikiCategoryListEntity) {
                addTab(new WikiFragment(mActivity,
                        ((WikiCategoryListEntity) listObject.get(i))));
            }*/
        }
    }

    public void addNullFragment() {
        CategorysEntity cate = new CategorysEntity();
        cate.setName("连接错误");
        tabs.add(cate);
        addTab(new HttpErrorFragment());
    }

    public void Clear() {
        mFragments.clear();
        tabs.clear();
    }

    public void addTab(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs.get(position).getName();
    }

    @Override
    public Fragment getItem(int arg0) {
        return mFragments.get(arg0);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    //

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
    }
}
